package srzy.module

import scala.collection.immutable.Range
import scala.collection.mutable.ListBuffer

object CdrComp extends AbsCaseDeal with srzy.common.Logging {

  //  def getIntList(s: String, c: (String, List[Int])) = {
  //    if (s == "") {
  //      List.empty[Int]
  //    } else {
  //      val len = srzy.common.split(s, """\|""").length
  //      logger.debug("len=" + len)
  //
  //      val intl = c._1 match {
  //        case "+" => {
  //          List(0 until len: _*).filter(c._2.indexOf(_) != -1)
  //        }
  //        case "-" => {
  //          List(0 until len: _*).filter(c._2.indexOf(_) == -1)
  //        }
  //        case "*" => {
  //          List(0 until len: _*)
  //        }
  //      }
  //      logger.debug("intList=" + intl)
  //      intl
  //    }
  //  }
  def getIntList(s: String, cc: Option[(String, List[Int])]) = {
    val len = srzy.common.split(s, """\|""").length
    cc match {
      case Some(c) => if (s == "") {
        List.empty[Int]
      } else {
        logger.debug("len=" + len)

        val intl = c._1 match {
          case "+" => {
            List(0 until len: _*).filter(c._2.indexOf(_) != -1)
          }
          case "-" => {
            List(0 until len: _*).filter(c._2.indexOf(_) == -1)
          }
          case "*" => {
            List(0 until len: _*)
          }
        }
        logger.debug("intList=" + intl)
        intl
      }
      case None => List(0 until len: _*)
    }
  }

  def getIntList(len: Int, cc: Option[(String, List[Int])]) = {
    cc match {
      case Some(c) =>
        logger.debug("len=" + len)
        val intl = c._1 match {
          case "+" => {
            List(0 until len: _*).filter(c._2.indexOf(_) != -1)
          }
          case "-" => {
            List(0 until len: _*).filter(c._2.indexOf(_) == -1)
          }
          case "*" => {
            List(0 until len: _*)
          }
        }
        logger.debug("intList=" + intl)
        intl
      case None => List(0 until len: _*)
    }
  }

  //  def cutStr(s: String, c: List[Int]) = {
  //
  //    val arrStr = srzy.common.split(s, """\|""")
  //    logger.debug("arrStr=" + arrStr)
  //
  //    c.map(i => {
  //      val s = arrStr.apply(i)
  //      logger.debug(i + "=" + s)
  //      if (s.indexOf(";") == -1) {
  //        s
  //      } else {
  //        s.split(";").toList.sortWith(_ > _).mkString(";")
  //      }
  //    }).mkString("|")
  //
  //  }

  def cutStr(s: String, c: List[Int]) = {

    val arrStr = srzy.common.split(s, """\|""")
    logger.debug("arrStr=" + arrStr)

    c.map(i => {
      val s = arrStr.apply(i)
      logger.debug(i + "=" + s)
      if (s.indexOf(";") == -1) {
        s
      } else {
        srzy.common.split(s, (";")).sortWith(_ > _).mkString(";")
      }
    }).mkString("|")
  }

  def deal = {
    val caseType = getCaseInfo.caseType
    logger.debug("CaseType:" + getCaseInfo.caseType)
    val l: List[String] = List("Normal", "Err", "Trans")
    for (e <- l) {
      val v = typeToCdrMap.getOrElse(e, Map()).getOrElse(caseType, ("*", List()))
      logger.debug("CdrType:" + e + ",caseType:" + getCaseInfo.caseType + "para:" + v)

      e match {
        case "Normal" => {
          if (getCaseInfo.normalCdr.stdFull.length != getCaseInfo.normalCdr.realFull.length) {
            logger.debug("normalCdr.stdFull length not equal to normalCdr.realFull length")
            fail
          } else {

            var stdCuttedCdrs0 = ListBuffer.empty[String]
            var realCuttedCdrs0 = ListBuffer.empty[String]
            (0 until getCaseInfo.normalCdr.stdFull.size).toList.map(i => {
              val stdFullCdr = getCaseInfo.normalCdr.stdFull.apply(i)
              val realFullCdr = getCaseInfo.normalCdr.realFull.apply(i)
              val minLen = Math.min(srzy.common.split(stdFullCdr, """\|""").length,
                srzy.common.split(realFullCdr, """\|""").length)
              val intList = getIntList(minLen, Some(v))

              stdCuttedCdrs0.append(cutStr(stdFullCdr, intList))
              realCuttedCdrs0.append(cutStr(realFullCdr, intList))
            })

            getCaseInfo.normalCdr.stdCutted = stdCuttedCdrs0.toList
            getCaseInfo.normalCdr.realCutted = realCuttedCdrs0.toList

            if (getCaseInfo.normalCdr.stdCutted.sortWith(_ > _).toString
              != getCaseInfo.normalCdr.realCutted.sortWith(_ > _).toString) {
              logger.debug("cdrs are not the same")
              logger.debug("stdCuttedCdr:" + getCaseInfo.normalCdr.stdCutted)
              logger.debug("stdFullCdr:" + getCaseInfo.normalCdr.stdFull)
              logger.debug("realCuttedCdr:" + getCaseInfo.normalCdr.realCutted)
              logger.debug("realFullCdr:" + getCaseInfo.normalCdr.realFull)
              fail
            }

          }
        }

        case "Err" => {
          if (getCaseInfo.errCdr.stdFull.length != getCaseInfo.errCdr.realFull.length) {
            logger.debug("errCdr.stdFull length not equal to errCdr.realFull length")
            fail
          } else {
            //            val intl = getCaseInfo.errCdr.stdFull
            //              .map(getIntList(_, v)).sortWith(_.length < _.length).headOption.getOrElse(List.empty[Int])
            //            getCaseInfo.errCdr.stdCutted = getCaseInfo.errCdr.stdFull.map(cutStr(_, intl))
            //            getCaseInfo.errCdr.realCutted = getCaseInfo.errCdr.realFull.map(cutStr(_, intl))
            //            if (getCaseInfo.errCdr.stdCutted.sortWith(_ > _).toString
            //              != getCaseInfo.errCdr.realCutted.sortWith(_ > _).toString) {
            //              logger.debug("cdrs are not the same")
            //              logger.debug("stdCuttedCdr:" + getCaseInfo.errCdr.stdCutted)
            //              logger.debug("stdFullCdr:" + getCaseInfo.errCdr.stdFull)
            //              logger.debug("realCuttedCdr:" + getCaseInfo.errCdr.realCutted)
            //              logger.debug("realFullCdr:" + getCaseInfo.errCdr.realFull)
            //              fail
            //            }

            var stdCuttedCdrs0 = ListBuffer.empty[String]
            var realCuttedCdrs0 = ListBuffer.empty[String]
            (0 until getCaseInfo.errCdr.stdFull.size).toList.map(i => {
              val stdFullCdr = getCaseInfo.errCdr.stdFull.apply(i)
              val realFullCdr = getCaseInfo.errCdr.realFull.apply(i)
              val minLen = Math.min(srzy.common.split(stdFullCdr, """\|""").length,
                srzy.common.split(realFullCdr, """\|""").length)
              val intList = getIntList(minLen, Some(v))

              stdCuttedCdrs0.append(cutStr(stdFullCdr, intList))
              realCuttedCdrs0.append(cutStr(realFullCdr, intList))
            })

            getCaseInfo.errCdr.stdCutted = stdCuttedCdrs0.toList
            getCaseInfo.errCdr.realCutted = realCuttedCdrs0.toList

            if (getCaseInfo.errCdr.stdCutted.sortWith(_ > _).toString
              != getCaseInfo.errCdr.realCutted.sortWith(_ > _).toString) {
              logger.debug("cdrs are not the same")
              logger.debug("stdCuttedCdr:" + getCaseInfo.errCdr.stdCutted)
              logger.debug("stdFullCdr:" + getCaseInfo.errCdr.stdFull)
              logger.debug("realCuttedCdr:" + getCaseInfo.errCdr.realCutted)
              logger.debug("realFullCdr:" + getCaseInfo.errCdr.realFull)
              fail
            }

          }
        }

        case "Trans" => {
          if (getCaseInfo.transCdr.stdFull.length != getCaseInfo.transCdr.realFull.length) {
            logger.debug("transCdr.stdFull length not equal to transCdr.realFull length")
            fail
          } else {

            //            val intl = getCaseInfo.transCdr.stdFull
            //              .map(getIntList(_, v)).sortWith(_.length < _.length).headOption.getOrElse(List.empty[Int])
            //            getCaseInfo.transCdr.stdCutted = getCaseInfo.transCdr.stdFull.map(cutStr(_, intl))
            //            getCaseInfo.transCdr.realCutted = getCaseInfo.transCdr.realFull.map(cutStr(_, intl))
            //            if (getCaseInfo.transCdr.stdCutted.sortWith(_ > _).toString
            //              != getCaseInfo.transCdr.realCutted.sortWith(_ > _).toString) {
            //              logger.debug("cdrs are not the same")
            //              logger.debug("stdCuttedCdr:" + getCaseInfo.transCdr.stdCutted)
            //              logger.debug("stdFullCdr:" + getCaseInfo.transCdr.stdFull)
            //              logger.debug("realCuttedCdr:" + getCaseInfo.transCdr.realCutted)
            //              logger.debug("realFullCdr:" + getCaseInfo.transCdr.realFull)
            //              fail
            //            }
            var stdCuttedCdrs0 = ListBuffer.empty[String]
            var realCuttedCdrs0 = ListBuffer.empty[String]
            (0 until getCaseInfo.transCdr.stdFull.size).toList.map(i => {
              val stdFullCdr = getCaseInfo.transCdr.stdFull.apply(i)
              val realFullCdr = getCaseInfo.transCdr.realFull.apply(i)
              val minLen = Math.min(srzy.common.split(stdFullCdr, """\|""").length,
                srzy.common.split(realFullCdr, """\|""").length)
              val intList = getIntList(minLen, Some(v))

              stdCuttedCdrs0.append(cutStr(stdFullCdr, intList))
              realCuttedCdrs0.append(cutStr(realFullCdr, intList))
            })

            getCaseInfo.transCdr.stdCutted = stdCuttedCdrs0.toList
            getCaseInfo.transCdr.realCutted = realCuttedCdrs0.toList

            if (getCaseInfo.transCdr.stdCutted.sortWith(_ > _).toString
              != getCaseInfo.transCdr.realCutted.sortWith(_ > _).toString) {
              logger.debug("cdrs are not the same")
              logger.debug("stdCuttedCdr:" + getCaseInfo.transCdr.stdCutted)
              logger.debug("stdFullCdr:" + getCaseInfo.transCdr.stdFull)
              logger.debug("realCuttedCdr:" + getCaseInfo.transCdr.realCutted)
              logger.debug("realFullCdr:" + getCaseInfo.transCdr.realFull)
              fail
            }

          }
        }
      }
    }
  }

  def fail = {
    getCaseInfo.cdrCompResult = false
    getCaseInfo.caseResult = false
  }
}
